package com.bakabase.animation.controller;

import com.bakabase.animation.domain.Animation;
import com.bakabase.animation.domain.dto.AnimationDto;
import com.bakabase.animation.domain.dto.HomeDataDto;
import com.bakabase.animation.domain.vo.AnimationVo;
import com.bakabase.animation.service.AnimationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/animation")
@CrossOrigin(origins = "*", maxAge = 3600)
public class AnimationController {

    @Resource
    private AnimationService animationService;


    @GetMapping("/list")
    public IPage<AnimationDto> list(AnimationVo vo) {
        Page<AnimationDto> page = new Page<>();
        page.setCurrent(vo.getPageNum());
        page.setSize(vo.getPageSize());
        return animationService.selectList(page, vo);
    }

    @GetMapping("/homeList")
    public HomeDataDto homeList() {
        return animationService.selectHomeDataList();
    }


    @PostMapping("/insertOrUpdate")
    public int insert(@RequestBody Animation animation) {
        if (animation.getId() == null) {
            return animationService.insertNewAnimation(animation);
        }
        return animationService.updateAnimation(animation);
    }

    @GetMapping("/getOneAnimation/{id}")
    public AnimationDto getOneAnimation(@PathVariable("id") Long id) {
        return animationService.selectOne(id);
    }

    @PostMapping("/delete/{id}")
    public int delete(@PathVariable("id") Long id) {
        return animationService.deleteAnimation(id);
    }

}
